package cn.fyg.web.finance.module;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import cn.fyg.common.base.retv.Retv;
import cn.fyg.service.finance.domain.accountdetail.AccountdetailService;
import cn.fyg.service.finance.domain.accountdetail.BProjectDto;
import cn.fyg.service.finance.domain.accountdetail.BalanceDto;
import cn.fyg.service.finance.domain.deposit.DepositDto;
import cn.fyg.service.finance.domain.deposit.DepositPjct;
import cn.fyg.service.finance.domain.deposit.DepositService;
import cn.fyg.service.finance.domain.login.AccountDto;
import cn.fyg.service.finance.domain.login.LoginDto;
import cn.fyg.service.finance.domain.login.LoginService;
import cn.fyg.service.finance.domain.salary.ProjectDto;
import cn.fyg.service.finance.domain.salary.SalaryDto;
import cn.fyg.service.finance.domain.salary.SalaryService;
import cn.fyg.service.finance.domain.salary.SalarydetailDto;
import cn.fyg.service.finance.domain.salary.SearchBean;

@Controller
public class WebController {
	public List<AccountDto> accountInfo;

	@Autowired
	LoginService loginService;

	@Autowired
	SalaryService salaryService;

	@Autowired
	DepositService depositService;

	@Autowired
	AccountdetailService accountdetailService;

	@GetMapping("")
	public String home(Model map) {

		return "/index";
	}

	@GetMapping("e")
	public String error(Model map) {
		map.addAttribute("msg", "请先登录再进行操作！");
		return "/error";
	}

	@GetMapping(value = "login")
	public String login(Model map) {
		map.addAttribute("msg", null);
		return "/login";
	}

	@PostMapping(value = "acl")
	public String doLogin(LoginDto loginDto, Model model) {
		Retv<List<AccountDto>> result = this.loginService.login(loginDto.getUser(), loginDto.getPassword());
		if (result.fail()) {
			// redirectAttributes.addFlashAttribute(Constant.MESSAGE_NAME,
			// result.getInfo());
			model.addAttribute("msg", result.getInfo());
			return "/login";
		} else {

			// redirectAttributes.addFlashAttribute(Constant.MESSAGE_NAME, "登录成功");
			// model.addAttribute("msg", "登录成功");
			accountInfo = result.getData();
			model.addAttribute("accountlist", accountInfo);
			model.addAttribute("page", result.getPage());
			model.addAttribute("user", loginDto.getUser());
			return "/accountlist";
		}
	}

	@PostMapping(value = "balancelist")
	public String goBalance(SearchBean bean, String accountname, Model model) {
		model.addAttribute("msg", null);
		Retv<BalanceDto> result = this.accountdetailService.getAccountAndProjcets(bean.getAccountid(),
				bean.getAccountnumber());
		if (result.fail()) {
			model.addAttribute("msg", result.getInfo());
			return "/accountlist";
		} else {
			String desc = "账户余额: " + result.getData().getBalance();
			model.addAttribute("detail", result.getData().getProjects());
			model.addAttribute("desc", desc);
			model.addAttribute("accountid", bean.getAccountid());
			model.addAttribute("accountname", accountname);
			model.addAttribute("page", result.getPage());
			return "/balancelist";
		}
	}

	@PostMapping(value = "balancedetail")
	public String goBalanceDetail(SearchBean bean, String projectname, Model model) {
		model.addAttribute("msg", null);
		Retv<BProjectDto> result = this.accountdetailService.getProjcetDetail(bean.getAccountid(), bean.getProjectid());
		if (result.fail()) {
			model.addAttribute("msg", result.getInfo());
			return "/balancelist";
		} else {
			String desc = result.getData().getPeriodBalance() + "  " + result.getData().getPeriodDebitamount() + "  "
					+ result.getData().getPeriodCreditamount();
			model.addAttribute("desc", desc);
			model.addAttribute("getPeriodBalance", result.getData().getPeriodBalance());
			model.addAttribute("getPeriodDebitamount", result.getData().getPeriodDebitamount());
			model.addAttribute("getPeriodCreditamount", result.getData().getPeriodCreditamount());
			model.addAttribute("projectname", projectname);
			model.addAttribute("detail", result.getData().getProjects());
			model.addAttribute("page", result.getPage());
			return "/balancedetail";
		}
	}

	@PostMapping(value = "depositlist")
	public String goDeposit(SearchBean bean, Model model) {
		model.addAttribute("msg", null);
		Retv<DepositDto> result = this.depositService.getDepositlist(bean.getAccountnumber());
		if (result.fail()) {
			model.addAttribute("msg", result.getInfo());
			return "/accountlist";
		} else {
			String desc = result.getData().getTotalUnReceivedAmount() + "  " + result.getData().getTotalInvoiceAmount()
					+ "  " + result.getData().getTotalReceivedAmount();
			model.addAttribute("detail", result.getData().getDetail());
			model.addAttribute("desc", desc);
			model.addAttribute("accountnumber", bean.getAccountnumber());
			model.addAttribute("totalInitAssistBalance", result.getData().getTotalInitAssistBalance());
			model.addAttribute("totalInvoiceAmount", result.getData().getTotalInvoiceAmount());
			model.addAttribute("totalReceivedAmount", result.getData().getTotalReceivedAmount());
			model.addAttribute("totalUnReceivedAmount", result.getData().getTotalUnReceivedAmount());

			model.addAttribute("page", result.getPage());
			return "/depositlist";
		}
	}
	
	@PostMapping(value = "depositproject")
	public String goDepositProject(SearchBean bean, Model model) {
		model.addAttribute("msg", null);
		Retv<DepositPjct> result = this.depositService.getDepositProject(bean.getAccountnumber(), bean.getAmountType());
		if (result.fail()) {
			model.addAttribute("msg", result.getInfo());
			return "/depositlist";
		} else {
			model.addAttribute("detail", result.getData().getDetail());
			
			model.addAttribute("getTotalInvoiceAmount", result.getData().getTotalInvoiceAmount());
			model.addAttribute("getTotalReceivedAmount", result.getData().getTotalReceivedAmount());
			model.addAttribute("page", result.getPage());
			return "/depositproject";
		}
	}
	
	@PostMapping(value = "depositdetail")
	public String goDepositDetail(SearchBean bean, String projectname, Model model) {
		model.addAttribute("msg", null);
		Retv<DepositPjct> result = this.depositService.getDepositdetail(bean.getProjectnumber());
		System.out.println(result);
		if (result.fail()) {
			model.addAttribute("msg", result.getInfo());
			return "/depositlist";
		} else {
			String desc = result.getData().getTotalUnReceivedAmount() + "  " + result.getData().getTotalInvoiceAmount()
					+ "  " + result.getData().getTotalReceivedAmount();
			model.addAttribute("detail", result.getData().getDetail());
			model.addAttribute("desc", desc);
			model.addAttribute("getTotalUnReceivedAmountsc", result.getData().getTotalUnReceivedAmount());
			model.addAttribute("getTotalInvoiceAmount", result.getData().getTotalInvoiceAmount());
			model.addAttribute("getTotalReceivedAmount", result.getData().getTotalReceivedAmount());
			model.addAttribute("projectname", projectname);
			model.addAttribute("page", result.getPage());
			return "/depositdetail";
		}
	}

	@PostMapping(value = "salarylist")
	public String goSalary(SearchBean bean, Model model) {
		model.addAttribute("msg", null);
		Retv<List<ProjectDto>> result = this.salaryService.getAllProject(bean.getAccountnumber());
		if (result.fail()) {
			model.addAttribute("msg", result.getInfo());
			return "/accountlist";
		} else {
			model.addAttribute("projectlist", result.getData());
			model.addAttribute("accountnumber", bean.getAccountnumber());
			model.addAttribute("page", result.getPage());
			return "/salarylist";
		}
	}

	@PostMapping(value = "salarydetail")
	public String goSalaryDetail(SearchBean bean, String projectname, Model model) {
		model.addAttribute("msg", null);
		Retv<SalaryDto> result = this.salaryService.getSalary(bean.getAccountnumber(), bean.getProjectnumber());
		if (result.fail()) {
			model.addAttribute("msg", result.getInfo());
			return "/salarylist";
		} else {
			List<SalarydetailDto> detail = result.getData().getDetail();
			String totalSalary = result.getData().getTotalSalary();
			String totalReceivedAmount = result.getData().getTotalReceivedAmount();
			String rate = result.getData().getRate();
			String desc = "总工资: " + totalSalary + " 已收账款: " + totalReceivedAmount + " 比例：" + rate;
			// projectname = projectname + " 详情";
			model.addAttribute("detail", detail);
			model.addAttribute("desc", desc);
			model.addAttribute("totalSalary", totalSalary);
			model.addAttribute("totalReceivedAmount", totalReceivedAmount);
			model.addAttribute("rate", rate);
			model.addAttribute("projectname", projectname);
			model.addAttribute("totalSalary", totalSalary);
			model.addAttribute("totalReceivedAmount", totalReceivedAmount);
			model.addAttribute("rate", rate);
			model.addAttribute("page", result.getPage());
			return "/salarydetail";
		}
	}
}
